package com.kelvin.oocl.dataprocess.process;

import java.text.ParseException;
import java.util.Set;

import com.kelvin.oocl.dataprocess.entity.Customer;
import com.kelvin.oocl.dataprocess.operation.CustomerOpetation;
import com.kelvin.oocl.dataprocess.util.CustomerSortById;
import com.kelvin.oocl.dataprocess.util.CustomerSortByBirthDate;
import com.kelvin.oocl.dataprocess.util.DateUtil;
import com.kelvin.oocl.dataprocess.util.Expression;

/**
 * 处理数据
 * @author MAIKE
 *
 */
public class DataProcess {
	private CustomerOpetation opetation = new CustomerOpetation();
	
	public void process(String data) throws ParseException {
		if(data==null){
			return;
		}
		Customer customer = new Customer();
		String[] field = data.split("\\s+");
		if(field[0].matches(Expression.ID_FILTER)){
			customer.setId(field[0]);
		}else {
			return;
		}
		if(field[1].matches(Expression.NAME_FILTER)){
			customer.setName(field[1]);
		}else {
			return;
		}
		if(field[2].matches(Expression.EMAIL_FILTER)){
			customer.setEmail(field[2]);
		}else {
			return;
		}
		if(field[3].matches(Expression.SEX_FILTER)){
			customer.setSex(Boolean.parseBoolean(field[3]));
		}else {
			return;
		}
		if(field[4].matches(Expression.BIRTHDATE_FILTER)){
			customer.setBirthDate(DateUtil.strToDate(field[4], "yyyy-MM-dd"));
		}else {
			return;
		}
		if(customer!=null){
			opetation.add(customer);
		}
	}
	
	public Set<Customer> sortById(){
		return opetation.sort(new CustomerSortById());
	}
	
	public Set<Customer> sortByBirthDate(){
		return opetation.sort(new CustomerSortByBirthDate());
	}
}
